<template>
  <div>
    <f-search-form :more='true' @searchHandler='searchHandler'>
      <f-search-form-item label='退货单号' name='orderNo' />
      <f-search-form-item label='单位名称' name='customName' />
      <f-search-form-item label='单据日期' type='range' name='beginDate' slot='advanced' />
      <f-search-form-item label='发货单号' name='parentId'  slot='advanced'/>
      <f-search-form-item label='查询范围' name='searchRang' type='select' :show-all='false'  slot='advanced'  :data='api.common.searchRang' valueKey='value' label-key='label'  />
      <f-search-form-item label='部门' ref='dept' name='deptId' type='select' :data='api.hyorder.listByCompanyId' valueKey='deptId' label-key='deptName' />
    </f-search-form>
    <f-table ref='table' rowKey='orderId' :columns='columns' :scroll='{x:true}'
             :multiple='false' :operator='operator' :params='queryParam' :api='api.hyorder' handler='hyOrderTab4ListHandler'>
      <span slot='sendType' slot-scope='text, record, index'>
        <a-tag color='#2db7f5' v-if='text==1'>现款</a-tag>
        <a-tag color='#FF0000' v-if='text==2'>预收款</a-tag>
        <a-tag color='#87d068' v-if='text==3'>资保金</a-tag>
        <a-tag color='purple' v-if='text==4'>全额资信</a-tag>
      </span>
      <span slot='printTag' slot-scope='text, record, index'>
         <a-tag color='#87d068' v-if='text==0'>未打印</a-tag>
         <a-tag color='purple' v-if='text>=1'>已打印</a-tag>
      </span>
      <span slot='orderState' slot-scope='text, record, index'>
        <a-tag color='pink' v-if='text==0'>未提交</a-tag>
        <a-tag color='#87d068' v-if='text==1' @click='processImg(record)'>{{ record.taskName }}</a-tag>
        <a-tag color='#f50' v-if='text==2'>已审批</a-tag>
      </span>
      <span slot='action' slot-scope='text, record'>
          <template>
            <a @click='startProcess(record.orderId,"hyBackOrderStartProcessHandler")' v-if='record.orderState==0'>提交</a>
            <a @click='completeTask(record)' v-if="record.taskDefinitionKey&&record.taskDefinitionKey=='__initiator__'">提交</a>
            <span v-if="record.orderState==0 ||record.taskDefinitionKey=='__initiator__'">
              <a-divider type='vertical' />
            </span>
            <a href='javascript:;' @click='$refs.orderTab4DetailsDrawer.showDrawer(record)'>详情</a>
            <a-divider type='vertical' />
          </template>
          <a-dropdown>
            <a class='ant-dropdown-link'>更多<a-icon type='down' /></a>
            <a-menu slot='overlay'>
               <a-menu-item v-if='record.orderState==0 && !record.processInstanceId'>
                <a href='javascript:;' @click='deleteBackOrder(record,{companyId:record.companyId,hostName:record.hostName})'>删除</a>
              </a-menu-item>
               <a-menu-item v-if='record.sendState==0 && record.orderState!=0 && (record.taskName=="销售复核员"|| record.taskName=="开票员制单")'>
                 <a href='javascript:;' @click='deleteProcessInstance(record.processInstanceId,"hyBackOrderProcDeleteHandler")'>撤回审批</a>
              </a-menu-item>
              <!--未提单,已提交审批-->
              <a-menu-item v-if='record.sendState==0 && record.orderState==1 && record.revokeState==0 && (record.taskName!="销售复核员")'>
                <a href='javascript:;' @click='revokeProcess(record)'>申请退回</a>
              </a-menu-item>
              <a-menu-item v-if='record.orderState==2'>
                <a href='javascript:;' @click='syncOrderReturn(record)'>同步到待回款</a>
              </a-menu-item>
            
              <a-menu-item v-if='record.orderState!=0'>
                 <a href='javascript:;' @click='processImg(record)'>审批进度</a>
              </a-menu-item>
              <a-menu-item>
                <a href='javascript:;' @click='printOrder(record)'>打印退货单</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
    </f-table>
    <order-drawer ref='drawer' @success='onLoad'></order-drawer>
    <hy-order-tab4-details-drawer ref='orderTab4DetailsDrawer'></hy-order-tab4-details-drawer>
    <process-image ref='processImg' v-if='processImageVisible' :visible.sync='processImageVisible' :processInstanceId='processInstanceId'></process-image>
    <print-modal ref='printModal'></print-modal>
  </div>
</template>
<script>
import api from "../js/api"
import OrderDrawer from "./order_drawer"
import ProcessImage from "../../flowable/process/process_image"
import PrintModal from "@/views/module/haiying/print/print_modal"
import HyOrderTab4DetailsDrawer from "@/views/module/haiying/order/details/hyorder_tab4_details_drawer"
import { processMixins } from "@/core/mixins/processMixins"

export default {
  name: "Tab4",
  mixins: [processMixins],
  components: {
    HyOrderTab4DetailsDrawer,
    PrintModal,
    OrderDrawer,
    ProcessImage
  },
  data() {
    let me = this
    //操作按钮
    let operator = []
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {},
      activeKey: "0",
      // 表头
      columns: [
        {
          title: "序号",
          width: "60px",
          align: "center",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "订单编号",
          align: "left",
          width: "120px",
          dataIndex: "orderNo"
        },
        {
          title: "退货单号",
          align: "left",
          width: "120px",
          dataIndex: "outOrderNo"
        },
        {
          title: "退货日期",
          align: "center",
          width: "120px",
          dataIndex: "sendDate"
        },
        {
          title: "发货单号",
          align: "left",
          width: "120px",
          dataIndex: "parentOrderNo"
        },
        {
          title: "发货方式",
          align: "left",
          dataIndex: "sendType",
          scopedSlots: { customRender: "sendType" }
        },
        {
          title: "部门",
          align: "left",
          dataIndex: "deptName"
        },
        {
          title: "单位名称",
          align: "left",
          ellipsis: true,
          width: "130px",
          dataIndex: "customName"
        },
        {
          title: "业务员",
          align: "left",
          width: "100px",
          dataIndex: "saleManName"
        },
        {
          title: "业务编码",
          align: "left",
          width: "130px",
          dataIndex: "businessCode"
        },
        {
          title: "退货日期",
          align: "center",
          width: "100px",
          dataIndex: "sendDate"
        },
        {
          title: "制单日期",
          align: "center",
          width: "120px",
          dataIndex: "createDate"
        },
        {
          title: "制单人",
          align: "center",
          width: "100px",
          dataIndex: "nickName"
        },
        {
          title: "打印状态",
          align: "center",
          width: "80px",
          fixed: "right",
          dataIndex: "printTag",
          scopedSlots: { customRender: "printTag" }
        },
        {
          title: "订单状态",
          align: "center",
          width: "110px",
          fixed: "right",
          dataIndex: "orderState",
          scopedSlots: { customRender: "orderState" }
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "150px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator,
      processImageVisible: false,
      processInstanceId: null
    }
  },
  created() {
  },
  methods: {
    onLoad() {
      this.$refs.table.refresh()
    },
    printOrder(record) {
      this.$refs["printModal"].open(record.orderId, 5)
    },
    //重新提交
    completeTask(order) {
      this.http.confirm(this, {
        content: "确认提交订单吗?",
        url: api.tasktodo.complete,
        requestBody: true,
        data: {
          taskId: order.taskId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //同步订单明细到回款表
    syncOrderReturn(order) {
      this.http.confirm(this, {
        content: "确认同步订单明细到回款表吗?",
        url: api.hyorder.syncOrderReturn,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    deleteBackOrder(order) {
      this.http.confirm(this, {
        content: "确认删除退货申请单吗?",
        url: api.hyorder.deleteBackOrder,
        data: {
          orderId: order.orderId,
          parentId:order.parentId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //申请取回订单
    revokeProcess(order) {
      this.http.confirm(this, {
        content: "确认申请取回订单吗?",
        url: api.hyorder.revokeProcess,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //申请撤销审批
    cancelRevokeProcess(order) {
      this.http.confirm(this, {
        content: "确认取消申请撤销审批吗?",
        url: api.hyorder.cancelRevokeProcess,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    changeHandler(activeKey) {
      this.activeKey = activeKey
      this.$refs.table.refresh(true, { activeKey: this.activeKey })
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    }
  }
}
</script>
